/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

import Supporter.ImageSupporter;
import Supporter.SoundSupporter;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 *
 * @author Khoa
 */
public class SetNameCanvas extends AbstractCanvas {

    private Image gameBackward = null;
    private Image gamebuttonOK = null;
    private Image gameforward = null;
    private Image numbermodeimage = null;
    private Image uppermodeimage = null;
    private Image lowermodeimage = null;
    private Image currentmodeimage = null;
    private String keycode;
    private int lastposition = 0;
    private int position = 0;
    private int charindex = 0;
    private char savekey;
    private char currentkey;
    private boolean Uppercasemode = false;
    private boolean Numbermode = false;
    private char[] alphabet = new char[26];
    private char[] upcasealpha = new char[26];
    private char[] nameofpet = new char[7];
    private char[] specialchar = new char[14];
    private ImageSupporter imgSupporter;
    private Player myplayer;
    public SetNameCanvas(MainMIDlet midlet,Player ss) throws IOException, MediaException {
        main = midlet;

        System.out.println("Begin construct!");
        int i = 0;
        for (char z = 'a'; z <= 'z'; z++) {
            alphabet[i] = z;
            i++;
        }
        i = 0;
        for (char z = 'A'; z <= 'Z'; z++) {
            upcasealpha[i] = z;
            i++;
        }
        String s = ".,?!-()@/:_";
        specialchar = s.toCharArray();
        System.out.println("Load Res ");
        
        myplayer=ss;
        
        LoadResource();
        System.out.println("Initial ok");
    }

    public void LoadResource() throws IOException {
        imgSupporter = new ImageSupporter();
        background = imgSupporter.getImage("/name-background.jpg");
        gameBackward = imgSupporter.getImage("/left-button-arrow.png");
        gamebuttonOK = imgSupporter.getImage("/button-tick.png");
        gameforward = Image.createImage(Image.createImage("/left-button-arrow.png"), 0,0, gameBackward.getWidth(), gameBackward.getHeight(), Sprite.TRANS_MIRROR);
        
        numbermodeimage = imgSupporter.getImage("/numbermode.jpg");
        uppermodeimage = imgSupporter.getImage("/upcasemode.jpg");
        lowermodeimage = imgSupporter.getImage("/lowcasemode.jpg");
        currentmodeimage = Image.createImage(lowermodeimage);
        
    }

    public void paint(Graphics g) {
        g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
        g.drawImage(gameBackward, 0, 320, Graphics.LEFT | Graphics.BOTTOM);
        g.drawImage(gamebuttonOK, getWidth(), 320, Graphics.RIGHT | Graphics.BOTTOM);
        g.drawImage(gameforward, 50, 320, Graphics.LEFT | Graphics.BOTTOM);
        g.drawImage(currentmodeimage, 100, 320, Graphics.LEFT | Graphics.BOTTOM);
        g.setColor(0, 0, 0);
        for (int i = 0; i < 7; i++) {
            g.drawChar(nameofpet[i], (i + 1) * 20, 20, Graphics.LEFT | Graphics.TOP);
            g.fillRect((i + 1) * 20, 40, 15, 2);
        }

        g.fillTriangle((position + 1) * 20 + 5, 42, (position + 1) * 20, 47, (position + 1) * 20 + 10, 47);
    }

    protected void keyPressed(int keyCode) {
        if (keyCode > 0) {
            currentkey = (char) keyCode;
            if (lastposition == position) {
                if (currentkey == savekey) {
                    charindex++;
                } else {
                    savekey = currentkey;
                    charindex = 0;
                }
            } else {
                lastposition = position;
                savekey = currentkey;
                charindex = 0;
            }
            String value = String.valueOf(currentkey);
            char displaychar = ' ';
            if (value.equals("#")) {
                if (Uppercasemode == true && Numbermode == false) {
                    Uppercasemode = false;
                    currentmodeimage = Image.createImage(lowermodeimage);
                    //g.drawImage(currentmodeimage, 100, 320, Graphics.LEFT | Graphics.BOTTOM);
                    repaint(100, 270, 50, 50);
                } else if (Uppercasemode == false && Numbermode == false) {
                    Uppercasemode = true;
                    currentmodeimage = Image.createImage(uppermodeimage);
                    //g.drawImage(currentmodeimage, 100, 320, Graphics.LEFT | Graphics.BOTTOM);
                    repaint(100, 270, 50, 50);
                }
            } else if (value.equals("*")) {
                if (Numbermode == true) {
                    Numbermode = false;
                    Uppercasemode = true;
                    currentmodeimage = Image.createImage(uppermodeimage);
                    //g.drawImage(currentmodeimage, 100, 320, Graphics.LEFT | Graphics.BOTTOM);
                    repaint(100, 270, 50, 50);
                } else if (Numbermode == false) {
                    Numbermode = true;
                    currentmodeimage = Image.createImage(numbermodeimage);
                    //g.drawImage(currentmodeimage, 100, 320, Graphics.LEFT | Graphics.BOTTOM);
                    repaint(100, 270, 50, 50);
                }
            } else {
                displaychar = getvalue(charindex, value);
                nameofpet[position] = displaychar;
                //g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
                //g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
                repaint();
//                for (int i = 0; i < nameofpet.length; i++) {
//                    g.drawChar(nameofpet[i], (i + 1) * 20, 20, Graphics.LEFT | Graphics.TOP);
//                    g.fillRect((i + 1) * 20, 40, 15, 2);
//                    g.fillTriangle((position + 1) * 20 + 5, 42, (position + 1) * 20, 47, (position + 1) * 20 + 10, 47);
//                }
                //g.drawImage(gameBackward, 0, 320, Graphics.LEFT | Graphics.BOTTOM);
                //g.drawImage(gamebuttonOK, getWidth(), 320, Graphics.RIGHT | Graphics.BOTTOM);
                //g.drawImage(gameforward, 50, 320, Graphics.LEFT | Graphics.BOTTOM);
                //g.drawImage(currentmodeimage, 100, 320, Graphics.LEFT | Graphics.BOTTOM);
                //repaint((position + 1) * 20 - 1, 20 - 1, 21, 21);
                repaint(19, 19, 220, 33);
            }
        }
    }

    public char getvalue(int index, String string) {

        int temp = 0;
        if (Numbermode == false) {
            if (string.equals("0")) {
                return ' ';
            }
            int value = Integer.parseInt(string);
            if (value == 7 || value == 9) {
                index = index % 4;
            } else if (value != 1) {
                index = index % 3;
            } else {
                index = index % 11;
            }
            if (value > 1) {
                if (value < 8) {
                    temp = (value - 1) * 3 + (index - 3);
                } else {
                    temp = (value - 1) * 3 + (index - 2);
                }
                if (Uppercasemode) {
                    return upcasealpha[temp];
                } else {
                    return alphabet[temp];
                }
            }
            return specialchar[index];

        } else {
            return (string.charAt(0));
        }

    }
    protected void pointerPressed(int x, int y) {
        if (x < 50 && y > 272) 
        {
            try {
                myplayer.stop();
                myplayer.setLoopCount(1);
                myplayer.start();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
            gameBackward = imgSupporter.getImage("/left-button-arrow-press.png");
            repaint();
        } else //20 45  -> 30 55
        if (x > 192 && y > 272) {
            try {
                myplayer.stop();
                myplayer.setLoopCount(1);
                myplayer.start();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
            gamebuttonOK = imgSupporter.getImage("/button-tick-press.png");
            repaint();
        } else if (x > 50 && x < 100 && y > 272) {
            try {
                myplayer.stop();
                myplayer.setLoopCount(1);
                myplayer.start();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
            try {
                gameforward = Image.createImage(Image.createImage("/left-button-arrow-press.png"), 0,0, gameBackward.getWidth(), gameBackward.getHeight(), Sprite.TRANS_MIRROR);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
           // gameforward = imgSupporter.getImage("/right-button-arrow-press.png");
            repaint();
        } 
    }
    protected void pointerReleased(int x, int y) {
        gameBackward = imgSupporter.getImage("/left-button-arrow.png");
        try {
            gameforward = Image.createImage(Image.createImage("/left-button-arrow.png"),0,0, gameBackward.getWidth(), gameBackward.getHeight(), Sprite.TRANS_MIRROR);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        gamebuttonOK = imgSupporter.getImage("/button-tick.png");
        repaint(0,272,240,50);
        if (x < 50 && y > 272) //delete
        {

            position--;
            System.out.print("position=" + position + "]");
            if (position < 0) {
                position = 0;
            }
            
            repaint();
        } else //20 45  -> 30 55
        if (x > 192 && y > 272) {
            
            String finalname;
            finalname = new String(nameofpet);
            main.getPet().setName(finalname);
                try {
                    //System.out.println(main.getPet().getName());
                    main.SwitchTo(1);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            
        } else if (x > 50 && x < 100 && y > 272) {
            position++;
            if (position > 6) {
                position = 6;
            }
            
            
            repaint();

        } else if (x > 100 && x < 150 && y > 272) {
            if (Uppercasemode == true && Numbermode == false) {
                Uppercasemode = false;
                currentmodeimage = Image.createImage(lowermodeimage);
                repaint(100, 270, 50, 50);
            } else if (Uppercasemode == false && Numbermode == false) {
                Numbermode = true;
                currentmodeimage = Image.createImage(numbermodeimage);
                repaint(100, 270, 50, 50);
            } else {
                Numbermode = false;
                Uppercasemode = true;
                currentmodeimage = Image.createImage(uppermodeimage);
                repaint(100, 270, 50, 50);
            }
        };
    }
}
